﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using LayuiDemo.Models;


namespace LayuiDemo.tms.ViewModels.TruckVMs
{
    public partial class TruckVM : BaseCRUDVM<Truck>
    {
        public List<ComboSelectListItem> AllDriverTruckss { get; set; }
        [Display(Name = "DriverTrucks")]
        public List<string> SelectedDriverTrucksIDs { get; set; }

        public TruckVM()
        {
            SetInclude(x => x.DriverTrucks);
        }

        protected override void InitVM()
        {
            AllDriverTruckss = DC.Set<Driver>().GetSelectListItems(Wtm, y => y.DriverName);
            SelectedDriverTrucksIDs = Entity.DriverTrucks?.Select(x => x.DriverId.ToString()).ToList();
        }

        public override void DoAdd()
        {
            Entity.DriverTrucks = new List<DriverTruck>();
            if (SelectedDriverTrucksIDs != null)
            {
                foreach (var id in SelectedDriverTrucksIDs)
                {
                     DriverTruck middle = new DriverTruck();
                    middle.SetPropertyValue("DriverId", id);
                    Entity.DriverTrucks.Add(middle);
                }
            }
           
            base.DoAdd();
        }

        public override void DoEdit(bool updateAllFields = false)
        {
            Entity.DriverTrucks = new List<DriverTruck>();
            if(SelectedDriverTrucksIDs != null )
            {
                 foreach (var item in SelectedDriverTrucksIDs)
                {
                    DriverTruck middle = new DriverTruck();
                    middle.SetPropertyValue("DriverId", item);
                    Entity.DriverTrucks.Add(middle);
                }
            }

            base.DoEdit(updateAllFields);
        }

        public override void DoDelete()
        {
            base.DoDelete();
        }
    }
}
